package br.brpv.silvestrin.vendas.webservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import br.brpv.silvestrin.vendas.domain.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class ServerDataCacheService extends Service {
    private User currentUser = null;
    private int defaultCompany = -1;
    private ArrayList<Bank> banksList;
    private ArrayList<Company> companiesList;
    private ArrayList<Motorist> motoristList;
    private Map<Integer, ArrayList<Client>> companyClientsMap;
    private Map<Integer, ArrayList<Item>> companyItemsMap;
    private Map<Integer, CompanyParameters> companyParamsMap;
    private Map<Integer, ArrayList<Transportation>> companyTransportation;
    private Map<Integer, ArrayList<PaymentForm>> companyPaymentForm;
    private Map<Integer, ArrayList<Invoice>> companyInvoiceTypes;
    private Map<Integer, ArrayList<Vendor>> companyVendors;
    private Map<Integer, ArrayList<PriceTable>> companyPriceTables;
    private Map<Integer, ArrayList<Order>> companySavedOrdersMap;
    private Map<Integer, Map<Integer, ArrayList<Establishment>>> clientEstablishmentMap;
    private Map<Integer, Map<Integer, Establishment>> clientEstablishmentDefaultMap;
    private Map<Integer, ArrayList<UnitOfMeasurement>> companyUnitsOfMeasurement;

    private final IBinder mBinder = new LocalBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        companiesList = new ArrayList<Company>();
        companyClientsMap = new HashMap<Integer, ArrayList<Client>>();
        companySavedOrdersMap = new HashMap<Integer, ArrayList<Order>>();
        companyItemsMap = new HashMap<Integer, ArrayList<Item>>();
        companyParamsMap = new HashMap<Integer, CompanyParameters>();
        companyTransportation = new HashMap<Integer, ArrayList<Transportation>>();
        companyPaymentForm = new HashMap<Integer, ArrayList<PaymentForm>>();
        banksList = new ArrayList<Bank>();
        companyInvoiceTypes = new HashMap<Integer, ArrayList<Invoice>>();
        companyVendors = new HashMap<Integer, ArrayList<Vendor>>();
        motoristList = new ArrayList<Motorist>();
        companyPriceTables = new HashMap<Integer, ArrayList<PriceTable>>();
        clientEstablishmentMap = new HashMap<Integer, Map<Integer, ArrayList<Establishment>>>();
        clientEstablishmentDefaultMap = new HashMap<Integer, Map<Integer, Establishment>>();
        companyUnitsOfMeasurement = new HashMap<Integer, ArrayList<UnitOfMeasurement>>();
    }

    public IBinder onBind(Intent intent) {
        return mBinder;
    }



    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        public ServerDataCacheService getService(){
            // Return this instance of LocalService so clients can call public methods
            return  ServerDataCacheService.this;
        }
    }

    public User getCurrentUser() {
        return currentUser;
    }
    public void setCurrentUser(User user) {
        clearServerData();
        currentUser = user;

    }

    public int getDefaultCompany() {
        return defaultCompany;
    }

    public void setDefaultCompany(int defaultCompany) {
        this.defaultCompany = defaultCompany;
    }

    public ArrayList<Company> getCompaniesList() {
        return companiesList;
    }

    public void setCompaniesList(ArrayList<Company> companiesList) {
        this.companiesList = companiesList;
    }

    public void addClientListToCompany(int companyId, ArrayList<Client> clientsList){
        companyClientsMap.put(companyId, clientsList);
    }

    public ArrayList<Client> getClientList(int companyId){
        return companyClientsMap.get(companyId);
    }

    public void addEstablishmentToClient(int companyId, int clientId, ArrayList<Establishment> establishmentArrayList){
        Map<Integer, ArrayList<Establishment>> map = clientEstablishmentMap.get(companyId);
        if(map == null){
            map = new HashMap<Integer, ArrayList<Establishment>>();
            clientEstablishmentMap.put(companyId, map);
        }
        map.put(clientId, establishmentArrayList);
    }
    public ArrayList<Establishment> getEstablishmentList(int companyId, int clientId){
        Map<Integer, ArrayList<Establishment>> map = clientEstablishmentMap.get(companyId);
        if(map == null) return null;
        return map.get(clientId);
    }

    public void addEstablishmentDefaultToClient(int companyId, int clientId, Establishment establishment){
        Map<Integer, Establishment> map = clientEstablishmentDefaultMap.get(companyId);
        if(map == null){
            map = new HashMap<Integer, Establishment>();
            clientEstablishmentDefaultMap.put(companyId, map);
        }
        map.put(clientId, establishment);
    }

    public Establishment getEstablishmentDefault(int companyId, int clientId){
        Map<Integer, Establishment> map = clientEstablishmentDefaultMap.get(companyId);
        if(map == null) return null;
        return map.get(clientId);
    }

    public void addSavedOrderListToCompany(int companyId, ArrayList<Order> orderList){
        companySavedOrdersMap.put(companyId, orderList);
    }

    public ArrayList<Order> getSavedOrderList(int companyId){
        return companySavedOrdersMap.get(companyId);
    }

    public void addItemListToCompany(int companyId, ArrayList<Item> itemList){
        companyItemsMap.put(companyId, itemList);
    }

    public ArrayList<Item> getItemList(int companyId){
        return companyItemsMap.get(companyId);
    }

    public void addCompanyParams(int companyId, CompanyParameters params){
        companyParamsMap.put(companyId, params);
    }
    public CompanyParameters getCompanyParams(int companyId){
        return companyParamsMap.get(companyId);
    }


    public ArrayList<Transportation> getCompanyTransportation(int companyId) {
        return companyTransportation.get(companyId);
    }

    public void addCompanyTransportation(int companyId, ArrayList<Transportation> transportations) {
        this.companyTransportation.put(companyId,transportations);
    }

    public ArrayList<PaymentForm> getCompanyPaymentForm(int companyId) {
        return companyPaymentForm.get(companyId);
    }

    public void addCompanyPaymenForm(int companyId, ArrayList<PaymentForm> companyPaymentForm) {
        this.companyPaymentForm.put(companyId, companyPaymentForm);
    }


    public ArrayList<Bank> getBanksList() {
        return banksList;
    }

    public void setBanksList(ArrayList<Bank> banksList) {
        this.banksList = banksList;
    }

    public ArrayList<Invoice> getCompanyInvoiveTypes(int companyId) {
        return companyInvoiceTypes.get(companyId);
    }

    public void addCompanyInvoiceTypes(int companyId, ArrayList<Invoice> invoiceList) {
        this.companyInvoiceTypes.put(companyId, invoiceList);
    }

    public ArrayList<Vendor> getCompanyVendors(int companyId) {
        return companyVendors.get(companyId);
    }

    public void addCompanyVendors(int companyId, ArrayList<Vendor> vendorList) {
        this.companyVendors.put(companyId, vendorList);
    }

    public ArrayList<Motorist> getMotoristList() {
        return motoristList;
    }

    public void setMotoristList(ArrayList<Motorist> motoristList) {
        this.motoristList = motoristList;
    }

    public ArrayList<PriceTable> getCompanyPriceTable(int companyId) {
        return companyPriceTables.get(companyId);
    }

    public void addCompanyPriceTable(int companyId, ArrayList<PriceTable> priceTables) {
        this.companyPriceTables.put(companyId, priceTables);
    }

    public ArrayList<UnitOfMeasurement> getUnitsOfMeasurements(int companyId) {
        return companyUnitsOfMeasurement.get(companyId);
    }

    public void addUnitsOfMeasurement(int companyId, ArrayList<UnitOfMeasurement> unitOfMeasurementArrayList) {
       this.companyUnitsOfMeasurement.put(companyId, unitOfMeasurementArrayList);
    }

    private void clearServerData(){
        currentUser = null;
        defaultCompany = -1;
        companiesList.clear();
        companyClientsMap.clear();
        companySavedOrdersMap.clear();
        companyItemsMap.clear();
        companyTransportation.clear();
        companyPaymentForm.clear();
        banksList.clear();
        companyInvoiceTypes.clear();
        companyVendors.clear();
        companyUnitsOfMeasurement.clear();
    }
}
